﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace RRD2.Model
{
    public class SerialPort
    {
        public string GetPortName(string _portName)
        {
            return _portName.ToUpper();
        }

        public int GetBaudRate(string _baudRate)
        {
            int value = 9600;
            try
            {
                value = Convert.ToInt32(_baudRate);
            }
            catch (Exception ex)
            {
                DAL.Logger.WriteException(ex);
            }
            return value;
        }

        public int GetDataBits(string _dataBits)
        {
            int value = 8;
            try
            {
                value = Convert.ToInt32(_dataBits);
            }
            catch (Exception ex)
            {
                DAL.Logger.WriteException(ex);
            }
            return value;
        }

        public Parity GetParity(string _parity)
        {
            Parity value = Parity.None;
            _parity = _parity.ToLower();
            switch (_parity)
            {
                case "none":
                    value = Parity.None;
                    break;
                case "even":
                    value=Parity.Even;
                    break;
                case "mark":
                    value=Parity.Mark;
                    break;
                case "odd":
                    value = Parity.Odd;
                    break;
                case "space":
                    value = Parity.Space;
                    break;
                default:
                    value = Parity.None;
                    break;
            }
            return value;
        }

        public StopBits StopBits(string _stopBids)
        {
            StopBits stopBits = System.IO.Ports.StopBits.None;
            _stopBids = _stopBids.ToLower();
            switch (_stopBids)
            {
                case "none":
                    stopBits = System.IO.Ports.StopBits.None;
                    break;
                case "one":
                    stopBits= System.IO.Ports.StopBits.One;
                    break;
                case "onepointfive":
                    stopBits = System.IO.Ports.StopBits.OnePointFive;
                    break;
                case "two":
                    stopBits = System.IO.Ports.StopBits.Two;
                    break;
                default:
                    stopBits = System.IO.Ports.StopBits.None;
                    break;
            }
            return stopBits;
        }

        public Handshake Handshake(string _handshake)
        {
            Handshake handshake = System.IO.Ports.Handshake.None;
            _handshake = _handshake.ToLower();
            switch(_handshake)
            {
                case "none":
                    handshake = System.IO.Ports.Handshake.None;
                    break;
                case "requesttosend":
                    handshake = System.IO.Ports.Handshake.RequestToSend;
                    break;
                case "requesttosendxonxoff":
                    handshake = System.IO.Ports.Handshake.RequestToSendXOnXOff;
                    break;
                case "xonxoff":
                    handshake = System.IO.Ports.Handshake.XOnXOff;
                    break;
                default:
                    handshake = System.IO.Ports.Handshake.None;
                    break;
            }
            return handshake;
        }

    }
}
